package com.kun.controller.foreign;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.entity.Blog;
import com.kun.result.Result;
import com.kun.service.impl.BlogServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @descriptions:
 * @author: SunKun
 * @date: 2021/6/20 0020 10:04
 * @version: 1.0
 */
@Controller
@Slf4j //日志
@CrossOrigin  //允许跨域
@ResponseBody
@RequestMapping("/kun/foreign/index")
public class IndexController {
    @Autowired
    private BlogServiceImpl blogService;
    private static final int SIZE = 5;

    /**
     * 按博客类别或时间进行分类
     * @param
     * @param
     * @param
     * @return
     */
    @PostMapping("/queryBlogByPage")
    public Result queryBlogByPage(@RequestBody Blog blog,
                                              @RequestParam(defaultValue = "1") Long current,
                                              @RequestParam int index){
        //创建分页对象，指定当前页码及每页显示数量
        Page<Blog> page = new Page<>(current,SIZE);
        //分页查询
        IPage<Blog> blogIPage = blogService.queryBlogByPage(page,blog,index);
        //获取博客数据列表
        List<Blog> blogList = blogIPage.getRecords();
        return Result.ok().data("blogList",blogList).data("totalNum",blogIPage.getTotal());
    }

    /**
     * 分页查询
     * @param current
     * @return
     */
    @PostMapping("/selectPage")
    public Result selectPage(@RequestParam int current){
        Page<Blog> page = blogService.queryPage(current, SIZE);
        return Result.ok().data("blogList",page.getRecords()).data("totalNum",page.getTotal());
    }

    /**
     * 默认分页查询
     * @return
     */
    @PostMapping("/defaultSelectPage")
    public Result defaultSelectPage(){
        Page<Blog> page = blogService.queryPage(1, SIZE);
        return Result.ok().data("blogList",page.getRecords()).data("totalNum",page.getTotal());
    }
}
